package com.daixin.blog.service;

import com.daixin.blog.dto.PageinationDTO;
import com.daixin.blog.dto.QuestionDTO;
import com.daixin.blog.exception.CustomizeErrorCode;
import com.daixin.blog.exception.CustomizeException;
import com.daixin.blog.mapper.QuestionExMapper;
import com.daixin.blog.mapper.QuestionMapper;
import com.daixin.blog.mapper.UserMapper;
import com.daixin.blog.model.Question;
import com.daixin.blog.model.QuestionExample;
import com.daixin.blog.model.User;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Author daixin
 */
@Service
public class QuestionService {
    @Autowired
    private QuestionMapper questionMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private QuestionExMapper questionExMapper;

    /**
     * 将问题信息和用户信息封装在一起
     * @return
     * @param page
     * @param size
     */
    public PageinationDTO selectQuestion(Integer page, Integer size) {
        PageinationDTO pageinationDTO = new PageinationDTO();
        Integer totalPage;
        Integer totalCount = (int)questionMapper.countByExample(new QuestionExample());
        if(totalCount % size ==0){
            totalPage = totalCount / size;
        }else {
            totalPage= (totalCount / size) + 1;
        }
        if (page < 1) {                //防止页码越界
            page = 1;
        }
        if (page > totalPage) {
            page = totalPage;
        }
        pageinationDTO.setPageinationDTO(totalPage, page);
        Integer offset = size * (page - 1);
        QuestionExample example = new QuestionExample();
        example.setOrderByClause("gmt_creat desc");
        List<Question> questionsList = questionMapper.selectByExampleWithRowbounds(example, new RowBounds(offset, size));
        List<QuestionDTO> questionDTOList = new ArrayList<>();
        for (Question question : questionsList) {
            QuestionDTO questionDTO = new QuestionDTO();
            User user = userMapper.selectByPrimaryKey(question.getCreator()); //获取用户id
            BeanUtils.copyProperties(question,questionDTO); //springboot 提供复制类的参数的方法
            questionDTO.setDescription(question.getDescription());
            questionDTO.setUser(user);
            questionDTOList.add(questionDTO);
        }
        pageinationDTO.setQuestions(questionDTOList);
        return pageinationDTO;
    }

    /**
     * 查询我的问题
     * @param page
     * @param size
     * @param id
     * @return
     */
    public PageinationDTO selectQuestionById(Integer page, Integer size,Long id) {
        PageinationDTO pageinationDTO = new PageinationDTO();
        Integer totalPage;
        QuestionExample questionExample = new QuestionExample();
        questionExample.createCriteria()
                .andCreatorEqualTo(id);
        Integer totalCount = (int)questionMapper.countByExample(questionExample);
        if(totalCount% size ==0){
            totalPage = totalCount / size;
        }else {
            totalPage= (totalCount / size) + 1;
        }
        if (page < 1) {                //防止页码越界
            page = 1;
        }
        if (page > totalPage) {
            page = totalPage;
        }
        pageinationDTO.setPageinationDTO(totalPage, page);
        Integer offset = size * (page - 1);
        QuestionExample example = new QuestionExample();
        example.createCriteria()
                .andCreatorEqualTo(id);
        example.setOrderByClause("gmt_creat desc");
        List<Question> questionsList = questionMapper.selectByExampleWithRowbounds(example, new RowBounds(offset, size));
        List<QuestionDTO> questionDTOList = new ArrayList<>();
        for (Question question : questionsList) {
            QuestionDTO questionDTO = new QuestionDTO();
            User user = userMapper.selectByPrimaryKey(question.getCreator()); //获取用户id
            BeanUtils.copyProperties(question,questionDTO); //springboot 提供复制类的参数的方法
            questionDTO.setUser(user);
            questionDTOList.add(questionDTO);
        }
        pageinationDTO.setQuestions(questionDTOList);
        return pageinationDTO;
    }

    /***
     * 通过id来获取问题信息
     * @param id
     * @return
     */
    public QuestionDTO findById(Long id){
        Question question = questionMapper.selectByPrimaryKey(id);
        if(question==null){
            throw new CustomizeException(CustomizeErrorCode.QUESTIONS_NOT_FOUND); //如果问题号不存在 抛出异常
        }
        QuestionDTO questionDTO = new QuestionDTO();
        User user = userMapper.selectByPrimaryKey(question.getCreator()); //获取用户id
        BeanUtils.copyProperties(question,questionDTO);
        questionDTO.setUser(user);
        return questionDTO;
    }

    /**
     * 增加阅读数
     * @param id
     */
    public  void incView(Long id){
        Question question = new Question();
        question.setId(id);
        question.setViewCount(1);
        questionExMapper.incView(question);
    }


}
